﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using stdole;
using VisionPPT.Properties;
using Office = Microsoft.Office.Core;
using Powerpoint = Microsoft.Office.Interop.PowerPoint;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new VisionRibbon();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace VisionPPT
{
    [ComVisible(true)]
    public class VisionRibbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public VisionRibbon()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("VisionPPT.VisionRibbon.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226
        public void OnColorButtonClick(Office.IRibbonControl control)
        {
            var textRange = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange;
            if (textRange.Length > 0) { 
                textRange.Font.Name = "Lucida Console";
            }
            if (control.Id.Equals("btnBlue")) { textRange.Font.Color.RGB = Color.FromArgb(129, 77, 25).ToArgb(); }
            else if (control.Id.Equals("btnLightBlue")) { textRange.Font.Color.RGB = Color.FromArgb(198, 172, 75).ToArgb(); }
            else if (control.Id.Equals("btnGreen")) { textRange.Font.Color.RGB = Color.FromArgb(89, 187, 155).ToArgb(); }
            else if (control.Id.Equals("btnRed")) { textRange.Font.Color.RGB = Color.FromArgb(77, 80, 192).ToArgb(); }
            else if (control.Id.Equals("btnPurple")) { textRange.Font.Color.RGB = Color.FromArgb(162, 100, 128).ToArgb(); }
            else if (control.Id.Equals("btnGrey")) { textRange.Font.Color.RGB = Color.FromArgb(153, 153, 153).ToArgb(); }
            else if (control.Id.Equals("btnOrange")) { textRange.Font.Color.RGB = Color.FromArgb(70, 150, 247).ToArgb(); }
        }

        public IPictureDisp GetFontImage(Office.IRibbonControl control)
        {
            IPictureDisp picture = PictureDispConverter.ToIPictureDisp(Resources.Transparent);
            if (control.Id.Equals("btnBlue")) { picture = PictureDispConverter.ToIPictureDisp(Resources.Blue); }
            else if (control.Id.Equals("btnLightBlue")) { picture = PictureDispConverter.ToIPictureDisp(Resources.LightBlue); }
            else if (control.Id.Equals("btnGreen")) { picture = PictureDispConverter.ToIPictureDisp(Resources.Green); }
            else if (control.Id.Equals("btnRed")) { picture = PictureDispConverter.ToIPictureDisp(Resources.Red); }
            else if (control.Id.Equals("btnPurple")) { picture = PictureDispConverter.ToIPictureDisp(Resources.Purple); }
            else if (control.Id.Equals("btnGrey")) { picture = PictureDispConverter.ToIPictureDisp(Resources.Grey); }
            else if (control.Id.Equals("btnOrange")) { picture = PictureDispConverter.ToIPictureDisp(Resources.Orange); }
            return picture;
        }

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
